//
//  TimeSelectPickerView.m
//  Btpin
//
//  Created by Vic on 2019/3/13.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "TimeSelectPickerView.h"
#import "UIDatePicker+Helper.h"

@interface TimeSelectPickerView ()
{
    NSString *pickerSelectString;
}
@end

@implementation TimeSelectPickerView

- (instancetype)initWithTarget:(id)target time:(nullable NSString *)time timeSelectPickerType:(TimeSelectPickerType)type dateFormat:(nullable NSString *)dateFormat {
    self = [super init];
    if (self) {
        if (time && time.length>0) {
            pickerSelectString = time;
        } else {
//            pickerSelectString = [UIToolsControl getCurrentTime];
//            pickerSelectString = @"2001-12-31";
            
            switch (type) {
                case TimeSelectPickerTypeOfDefault: {
                    pickerSelectString = [UIToolsControl getCurrentTime];
                } break;
                case TimeSelectPickerTypeOfBirthday: {
                    pickerSelectString = @"2001-12-31";
                } break;
                case TimeSelectPickerTypeOfWorking: {
                    pickerSelectString = [UIToolsControl getCurrentTime];
                } break;
                default:
                    break;
            }
        }
        [self initUITimeSelectPickerType:type dateFormat:dateFormat];
        self.delegate = target;
    }
    return self;
}
- (void)initUITimeSelectPickerType:(TimeSelectPickerType)type dateFormat:(nullable NSString *)dateFormat {
    self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.backgroundColor = [UIColor colorWithWhite:0.01 alpha:0.5];
    [self addTarget:self action:@selector(cancelBtnTap) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    UIView *bgView = [UIToolsControl initViewWithFrame:CGRectMake(0, HEIGHT-SIMEmojKeyBoardkBottomNOSafeArea_Height-PickerHeight-50*Scale, WIDTH, PickerHeight+50*Scale) backgroundColor:COLOR_OF_WHITE];
    [self addSubview:bgView];
    bgView.userInteractionEnabled = YES;
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, WIDTH, PickerHeight)];
    datePicker.backgroundColor = COLOR_OF_WHITE;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    switch (type) {
        case TimeSelectPickerTypeOfDefault: {
            formatter.dateFormat = DateFormatYYYYMMDD;
        } break;
        case TimeSelectPickerTypeOfBirthday: {
            formatter.dateFormat = DateFormatYYYYMMDD;
            NSDate *maxDate = [formatter dateFromString:@"2001-12-31"];
            NSDate *minDate = [formatter dateFromString:@"1969-1-1"];
            datePicker.maximumDate = maxDate;
            datePicker.minimumDate = minDate;
        } break;
        case TimeSelectPickerTypeOfWorking: {
            formatter.dateFormat = DateFormatYYYYMMDD;
            NSDate *maxDate = [formatter dateFromString:[UIToolsControl getCurrentTime]];
            datePicker.maximumDate = maxDate;
        } break;
        default:
            break;
    }
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    datePicker.datePickerMode = UIDatePickerModeDate;
//    [datePicker setDate:[NSDate date] animated:YES];
    [datePicker setDate:[formatter dateFromString:[UIToolsControl getCurrentTime]] animated:YES];
//    NSDate *maxDate = [formatter dateFromString:pickerSelectString];
    
    [datePicker clearSpearatorLineAndSetTextColor];
    
    [datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    [bgView addSubview:datePicker];
    
    UIButton *cancelBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, ViewHeight(bgView)-50*Scale, WIDTH*0.5, 50*Scale) imageName:nil target:self action:@selector(cancelBtnTap) title:@"取消" titleColor:THEMECOLOR_OF_TEXT_9a9a9a];
    cancelBtn.titleLabel.font = Font(Font_17);
    [bgView addSubview:cancelBtn];
    [cancelBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(EndX(cancelBtn), StartY(cancelBtn), ViewWidth(cancelBtn), ViewHeight(cancelBtn)) imageName:nil target:self action:@selector(okBtnTap) title:@"确认" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_17);
    [bgView addSubview:okBtn];
    [okBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:(BorderSideTypeTop | BorderSideTypeLeft)];
}
- (void)dateChange:(UIDatePicker *)datePicker {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *dateStr = [formatter stringFromDate:datePicker.date];
    pickerSelectString = dateStr;
}
- (void)cancelBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(timeSelectPickerViewCancel:)]) {
        [self.delegate timeSelectPickerViewCancel:self];
    }
}
- (void)okBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(timeSelectPickerViewOK:timeSelectPickerView:)]) {
        [self.delegate timeSelectPickerViewOK:pickerSelectString timeSelectPickerView:self];
    }
}

@end
